#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include "myFont.h"
#include "lv_compment.h"
#include "esp_log.h"
#include "esp_system.h"
#include "lv_port_indev.h"
#include "mqtt_client.h"
#include "file_server.h"
#include "wificonnet.h"
#define ANIEND  while(lv_anim_count_running())lv_task_handler();//等待动画完成
#include "page.h"
LV_FONT_DECLARE(myFont);
extern lv_obj_t* scr;
extern lv_obj_t* scr_body;
extern esp_mqtt_client_handle_t client;
extern httpd_handle_t server ;//文件服务句柄
lv_group_t * group_button;
lv_obj_t *tabview;
lv_obj_t *tab1,*tab2,*tab3;
uint8_t clock_change=0;//时间是否发生改变
extern struct _blbl_sys_t blbl_sys_t;
struct {
    lv_obj_t* label_wifi;
    lv_obj_t* sw_wifi;
    lv_obj_t* labe_mqtt;
    lv_obj_t* sw_mqtt;
    lv_obj_t* page;
    lv_obj_t* labe_info;
}tab1_t;
struct {
    lv_obj_t* label_time;
    lv_obj_t* sw_time;
    lv_obj_t* cont;
    lv_obj_t* table;
}tab2_t;
struct {
    lv_obj_t* label_temp;
    lv_obj_t* sw_temp;
    lv_obj_t* page;
    lv_obj_t* set_info;
}tab3_t;

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        int8_t i=lv_switch_get_state(obj);
        if(obj==tab1_t.sw_wifi)//网页管理
        {
          if(i)//开启web
           {
               if(server==NULL)
               {
                    if(start_file_server("/spiffs")!=ESP_OK)//启动文件系统
                    {
                        blbl_sys_t.state.web=0;               
                    }else
                    {
                        ESP_LOGI("WEB_SET","web服务开启");
                        lv_ico_web_show(1);//显示web图标
                        blbl_sys_t.state.web=1;
                        web_switch_save(1);                        
                    }
               }
               else
                {
                    ESP_LOGI("WEB_SET","web服务已经开启");
                }

           }else
           {
            lv_ico_web_show(0);//删除web图标 
            blbl_sys_t.state.web=0;
            web_switch_save(0);
            stop_webserver();
            ESP_LOGI("WEB","web服务准备关闭");
           }

        }
        else if(obj==tab1_t.sw_mqtt)//mqtt开关
        {
            if(i)
            {
                blbl_sys_t.state.mqtt=1;
                mqtt_switch_save(1);
                if(client!=NULL)
                {
                esp_mqtt_client_start(client);    
                lv_ico_mqtt_show(1);//添加mqtt图标                                  
                }
                else
                lv_blbl_mqtt_init();

                
            }else
            {
                mqtt_switch_save(0);
                blbl_sys_t.state.mqtt=0;
                if(client!=NULL)
                esp_mqtt_client_stop(client);   
                lv_ico_mqtt_show(0);//删除mqtt图标

            }
        }
        else if(obj==tab2_t.sw_time)//如果是闹钟总开关
        {
            // if(i)//开启
            // {
            clock_switch_save(i);
            blbl_sys_t.state.clock=i; 
            lv_ico_clock_show(i);//删除mqtt图标              
            // }
            // else
            // {
            // clock_switch_save(0);
            // blbl_sys_t.state.clock=0;
            // lv_ico_clock_show(0);//删除mqtt图标

            // }
            
        }
        else if (obj==tab3_t.sw_temp)
        {
           blbl_sys_t.state.upcloud=i;
            lv_ico_temp_show(i);
            web_temp_save(i);
        }
        
       
    }
}
void tab_wifi_init(lv_obj_t * obj)
{

    /******wifi_switch********/
    tab1_t.label_wifi = lv_label_create(obj, NULL);
    lv_obj_set_style_local_text_font(tab1_t.label_wifi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(tab1_t.label_wifi,  "网页" );
    lv_obj_set_pos(tab1_t.label_wifi,10,10);
    lv_label_set_align(tab1_t.label_wifi, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/

    tab1_t.sw_wifi = lv_switch_create(obj, NULL);
    if(blbl_sys_t.state.web)
    lv_switch_on(tab1_t.sw_wifi, LV_ANIM_ON);
    lv_obj_set_event_cb(tab1_t.sw_wifi, event_handler);
    lv_obj_set_size(tab1_t.sw_wifi,30,16);
    lv_obj_set_pos(tab1_t.sw_wifi,50,10);
    /*******mqtt_switch*******/
    tab1_t.labe_mqtt = lv_label_create(obj, NULL);
    lv_obj_set_style_local_text_font(tab1_t.labe_mqtt,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(tab1_t.labe_mqtt,  "mqtt" );
    lv_obj_set_pos(tab1_t.labe_mqtt,130,8);

    tab1_t.sw_mqtt = lv_switch_create(obj, NULL);
    if(blbl_sys_t.state.mqtt)
    lv_switch_on(tab1_t.sw_mqtt, LV_ANIM_ON);
    lv_obj_set_size(tab1_t.sw_mqtt,30,16);
    lv_obj_set_pos(tab1_t.sw_mqtt,170,10);
    lv_obj_set_event_cb(tab1_t.sw_mqtt, event_handler);
    /**********page*********/
    tab1_t.page = lv_page_create(obj, NULL);
    lv_obj_set_size(tab1_t.page, 200, 60);
    lv_obj_set_pos(tab1_t.page,20,30);

    lv_obj_t * label = lv_label_create(tab1_t.page, NULL);
    //lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);            /*Automatically break long lines*/
    lv_obj_set_width(label, lv_page_get_width_fit(tab1_t.page));          /*Set the label width to max value to not show hor. scroll bars*/
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    char str[100];
    sprintf(str,"网络SSID:%s\nIP:%s",get_ssid(),get_ip());
    lv_label_set_text(label, (const char *)str);

}

static void event_handler_clock(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        clock_change=1;
    }
}
struct _tab2_time_t{
    lv_obj_t* cont;
    lv_obj_t* list_shi;
    lv_obj_t* list_fen1;
    lv_obj_t* list_fen2;
    lv_obj_t* label_dian;
    lv_obj_t* sw;
}tab2_time_t[2];
void creat_time(uint8_t index)
{
    /*******下滑选择*********/
    tab2_time_t[index].list_shi = lv_dropdown_create(tab2_time_t[index].cont, NULL);
    lv_obj_set_style_local_border_opa(tab2_time_t[index].list_shi,LV_DROPDOWN_PART_MAIN,LV_STATE_DEFAULT,0);
    lv_obj_set_size(tab2_time_t[index].list_shi,20,30);
    lv_dropdown_set_symbol(tab2_time_t[index].list_shi, NULL);
    lv_dropdown_set_options(tab2_time_t[index].list_shi, "00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23");
    lv_obj_set_pos(tab2_time_t[index].list_shi,10,0);
    lv_obj_set_event_cb(tab2_time_t[index].list_shi, event_handler_clock);


    tab2_time_t[index].list_fen1 = lv_dropdown_create(tab2_time_t[index].cont, NULL);
    lv_obj_set_style_local_border_opa(tab2_time_t[index].list_fen1,LV_DROPDOWN_PART_MAIN,LV_STATE_DEFAULT,0);
    lv_obj_set_size(tab2_time_t[index].list_fen1,10,30);
    lv_dropdown_set_symbol(tab2_time_t[index].list_fen1, NULL);
    lv_dropdown_set_options(tab2_time_t[index].list_fen1,"0\n1\n2\n3\n4\n5");
    lv_obj_set_pos(tab2_time_t[index].list_fen1,40,0);
    lv_obj_set_event_cb(tab2_time_t[index].list_fen1, event_handler_clock);

    tab2_time_t[index].list_fen2 = lv_dropdown_create(tab2_time_t[index].cont, NULL);
    lv_obj_set_style_local_border_opa(tab2_time_t[index].list_fen2,LV_DROPDOWN_PART_MAIN,LV_STATE_DEFAULT,0);
    lv_obj_set_size(tab2_time_t[index].list_fen2,10,30);
    lv_dropdown_set_symbol(tab2_time_t[index].list_fen2, NULL);
    lv_dropdown_set_options(tab2_time_t[index].list_fen2, "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n");
    lv_obj_set_pos(tab2_time_t[index].list_fen2,50,0);
    lv_obj_set_event_cb(tab2_time_t[index].list_fen2, event_handler_clock);

    tab2_time_t[index].label_dian = lv_label_create(tab2_time_t[index].cont, NULL);
    lv_obj_set_style_local_text_font(tab2_time_t[index].label_dian,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(tab2_time_t[index].label_dian,  ":" );
    lv_obj_set_pos(tab2_time_t[index].label_dian,30,6);
    lv_label_set_align(tab2_time_t[index].label_dian, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/

    /*******on************/
    tab2_time_t[index].sw = lv_switch_create(tab2_time_t[index].cont, NULL);
    lv_obj_set_event_cb(tab2_time_t[index].sw, event_handler);
    lv_obj_set_size(tab2_time_t[index].sw,30,16);
    lv_obj_align(tab2_time_t[index].sw, NULL, LV_ALIGN_CENTER, 40, 0);
    lv_obj_set_pos(tab2_time_t[index].sw,140,8);

    lv_obj_set_style_local_bg_color(tab2_time_t[index].list_shi,LV_DROPDOWN_PART_MAIN,LV_STATE_FOCUSED,LV_COLOR_AQUA);
    lv_obj_set_style_local_bg_color(tab2_time_t[index].list_fen1,LV_DROPDOWN_PART_MAIN,LV_STATE_FOCUSED,LV_COLOR_AQUA);
    lv_obj_set_style_local_bg_color(tab2_time_t[index].list_fen2,LV_DROPDOWN_PART_MAIN,LV_STATE_FOCUSED,LV_COLOR_AQUA);
}
void tab_time_init(lv_obj_t * obj)
{
    /*********time_switch**********/
    tab2_t.label_time = lv_label_create(obj, NULL);
    lv_obj_set_style_local_text_font(tab2_t.label_time,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(tab2_t.label_time, "闹钟总开关");
    lv_obj_set_pos(tab2_t.label_time,30,5);

    tab2_t.sw_time = lv_switch_create(obj, NULL);
    lv_obj_set_event_cb(tab2_t.sw_time, event_handler);
    lv_obj_set_size(tab2_t.sw_time,30,16);
    lv_obj_set_pos(tab2_t.sw_time,150,5);
    if(blbl_sys_t.state.clock)//开启开关
    lv_switch_on(tab2_t.sw_time, LV_ANIM_ON);        

    /********time_info**********/
    uint8_t min,min_h,min_l,h;
    for(uint8_t i=0;i<2;i++)
    {
        tab2_time_t[i].cont = lv_cont_create(obj, NULL);
        lv_obj_set_size(tab2_time_t[i].cont,200,30);
        lv_obj_set_pos(tab2_time_t[i].cont,20,30+40*i);
        creat_time(i);
        if(blbl_sys_t.clock.clock[i].state)//开启开关
        lv_switch_on(tab2_time_t[i].sw, LV_ANIM_ON); 

        h=blbl_sys_t.clock.clock[i].h;
        min=blbl_sys_t.clock.clock[i].m; 
        min_h=min/10;
        min_l=min%10;               
        lv_dropdown_set_selected(tab2_time_t[i].list_shi,h);//更新时钟；
        lv_dropdown_set_selected(tab2_time_t[i].list_fen1,min_h);//更新时钟；
        lv_dropdown_set_selected(tab2_time_t[i].list_fen2,min_l);//更新时钟；
    }

}
/****
*
*
    温湿度上传间隔
    简介：使用方法 开源地址
*
***/

void tab_set_init(lv_obj_t * obj)
{
    /*********temp**********/
    tab3_t.label_temp = lv_label_create(obj, NULL);
    lv_obj_set_style_local_text_font(tab3_t.label_temp,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(tab3_t.label_temp, "温湿度上报");
    lv_obj_set_pos(tab3_t.label_temp,30,5);

    tab3_t.sw_temp = lv_switch_create(obj, NULL);
    if(blbl_sys_t.state.upcloud)
    lv_switch_on(tab3_t.sw_temp, LV_ANIM_ON);
    lv_obj_set_event_cb(tab3_t.sw_temp, event_handler);
    lv_obj_set_size(tab3_t.sw_temp,30,16);
    lv_obj_set_pos(tab3_t.sw_temp,150,5);

    /*******page*********/
    tab3_t.page = lv_page_create(obj, NULL);
    lv_obj_set_size(tab3_t.page, 200, 100);
    lv_obj_set_pos(tab3_t.page,20,30);

    lv_obj_t * label = lv_label_create(tab3_t.page, NULL);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);            /*Automatically break long lines*/
    lv_obj_set_width(label, lv_page_get_width_fit(tab3_t.page));          /*Set the label width to max value to not show hor. scroll bars*/
    lv_obj_set_style_local_text_font(label,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(label, "这是一个开源项目\n"
                             "硬件开源网址\n"
                             "http://balabala.com\n"
                             "软件开源网址\n"
                             "巴拉巴拉\n");
}
static void event_handler_tab(lv_obj_t * obj, lv_event_t event)
{
     if(event == LV_EVENT_VALUE_CHANGED) {
        int8_t i=lv_tabview_get_tab_act(obj);
        switch (i)
        {
        case 0:
            lv_group_remove_all_objs(group_button);
            lv_group_add_obj(group_button, tabview);//加入解码组 
            lv_group_add_obj(group_button, tab1_t.sw_wifi);//加入解码组            
            lv_group_add_obj(group_button, tab1_t.sw_mqtt);//加入解码组
            break;
        case 1:
            lv_group_remove_all_objs(group_button);
            lv_group_add_obj(group_button, tabview);//加入解码组 
            lv_group_add_obj(group_button, tab2_t.sw_time);//加入解码组  
            for(uint8_t i=0;i<2;i++)
            {
                lv_group_add_obj(group_button, tab2_time_t[i].list_shi);//加入解码组   
                lv_group_add_obj(group_button, tab2_time_t[i].list_fen1);//加入解码组 
                lv_group_add_obj(group_button, tab2_time_t[i].list_fen2);//加入解码组                             
                lv_group_add_obj(group_button, tab2_time_t[i].sw);//加入解码组                  
            }
            break;
        case 2:
            lv_group_remove_all_objs(group_button);
            lv_group_add_obj(group_button, tabview);//加入解码组 
            lv_group_add_obj(group_button, tab3_t.sw_temp);//加入解码组   
            break;
        default:
            break;
        }
    }
}

void lv_tabview_init(void)
{
    /*Create a Tab view object*/

    tabview = lv_tabview_create(scr, NULL);

    static lv_style_t style1;
    lv_obj_set_style_local_pad_all(tabview,LV_TABVIEW_PART_TAB_BTN,LV_STATE_DEFAULT,LV_DPI / 12);

    lv_obj_add_style(tabview, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
    lv_tabview_set_btns_pos(tabview, LV_TABVIEW_TAB_POS_TOP);

    tab1 = lv_tabview_add_tab(tabview, LV_SYMBOL_WIFI);
    tab2 = lv_tabview_add_tab(tabview, LV_SYMBOL_BELL);
    tab3 = lv_tabview_add_tab(tabview, LV_SYMBOL_SETTINGS);

    tab_wifi_init(tab1);
    tab_time_init(tab2);
    tab_set_init(tab3);
    lv_obj_set_event_cb(tabview, event_handler_tab);

}
void lv_calendar(void)
{
    lv_obj_t  * calendar = lv_calendar_create(lv_scr_act(), NULL);
    lv_obj_set_size(calendar, 240, 135);
    lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(calendar, event_handler);

    lv_obj_set_style_local_pad_all(calendar,LV_CALENDAR_PART_HEADER,LV_STATE_DEFAULT,LV_DPI / 32);
    lv_obj_set_style_local_pad_bottom(calendar,LV_CALENDAR_PART_DAY_NAMES,LV_STATE_DEFAULT,LV_DPI / 32);

  // lv_obj_set_style_local_pad_hor(calendar,LV_CALENDAR_PART_DATE,LV_STATE_DEFAULT,LV_DPI / 32);
   lv_obj_set_style_local_outline_pad(calendar,LV_CALENDAR_PART_DATE,LV_STATE_DEFAULT,LV_DPI / 32);

   // lv_obj_set_style_local_border_width(calendar,LV_CALENDAR_PART_DATE,LV_STATE_DEFAULT,LV_DPI / 32);
    lv_obj_set_style_local_pad_all(calendar,LV_CALENDAR_PART_DATE,LV_STATE_DEFAULT,0);

  //  lv_obj_set_style_local_outline_width(calendar,LV_CALENDAR_PART_DATE,LV_STATE_DEFAULT,1);

    /*Make the date number smaller to be sure they fit into their area*/
    lv_obj_set_style_local_text_font(calendar, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small());

    /*Set today's date*/
    lv_calendar_date_t today;
    today.year = 2018;
    today.month = 10;
    today.day = 23;

    lv_calendar_set_today_date(calendar, &today);
    lv_calendar_set_showed_date(calendar, &today);

    /*Highlight a few days*/
    static lv_calendar_date_t highlighted_days[3];       /*Only its pointer will be saved so should be static*/
    highlighted_days[0].year = 2018;
    highlighted_days[0].month = 10;
    highlighted_days[0].day = 6;

    highlighted_days[1].year = 2018;
    highlighted_days[1].month = 10;
    highlighted_days[1].day = 11;

    highlighted_days[2].year = 2018;
    highlighted_days[2].month = 11;
    highlighted_days[2].day = 22;

    lv_calendar_set_highlighted_dates(calendar, highlighted_days, 3);
}
compment_t page3;

static void group_init()
{
    group_button=lv_group_create();
    lv_group_add_obj(group_button, tab1_t.sw_mqtt);//加入解码组
    lv_group_add_obj(group_button, tab1_t.sw_wifi);//加入解码组
    lv_group_add_obj(group_button, tabview);//加入解码组 
    lv_button_set_group(group_button);
}
void page_load()
{
    page3.start=135;
    page3.end=0;
    page3.obj=tabview;
    page3.start_t=600;
    page3.end_t=600;
    page3.cb=(void *)lv_anim_path_ease_out;
    page3.funcb=(void *)lv_obj_set_y;
    page3.next=NULL;
    obj_move(&page3,1);
    ANIEND
    group_init();
}
void page3_save_clock()
{
    uint8_t s1,s2,h1,m1,h2,m2;
    if(clock_change)//时间改变
    {
        clock_change=0;
        h1=lv_dropdown_get_selected(tab2_time_t[0].list_shi);
        m1=lv_dropdown_get_selected(tab2_time_t[0].list_fen1)*10+lv_dropdown_get_selected(tab2_time_t[0].list_fen2);
        h2=lv_dropdown_get_selected(tab2_time_t[1].list_shi);
        m2=lv_dropdown_get_selected(tab2_time_t[1].list_fen1)*10+lv_dropdown_get_selected(tab2_time_t[1].list_fen2);
        s1=lv_switch_get_state(tab2_time_t[0].sw);
        s2=lv_switch_get_state(tab2_time_t[1].sw);
        blbl_sys_t.clock.clock[0].h=h1;
        blbl_sys_t.clock.clock[1].h=h2;
        blbl_sys_t.clock.clock[0].m=m1;
        blbl_sys_t.clock.clock[1].m=m2;
        blbl_sys_t.clock.clock[0].state=s1;
        blbl_sys_t.clock.clock[1].state=s2;
        save_clock(0,s1,h1,m1);
        save_clock(1,s2,h2,m2);        
    }
}
void page3_end()
{
    page3_save_clock();
    obj_move(&page3,0);
    ANIEND
    //lv_obj_del(tab2_time_t[0].cont);
    lv_obj_del(tabview);

}
void page3_start()
{
    lv_tabview_init();
    page_load();
  // lv_calendar();
    ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d  \n", esp_get_free_heap_size());
   // lv_obj_clean(scr);
}
